<template>
  <div class="official">
    <el-col v-bind="grid">
      <el-form ref="refOfficial" :model="model" :rules="rules" label-position="right" label-width="160px">
        <el-form-item prop="appId">
          <template v-slot:label>AppID  <el-tooltip class="item" effect="dark" content="AppID" placement="top-start"><i class="el-icon-info" /></el-tooltip></template>
          <el-input v-model="model.appId" placeholder="AppID"></el-input>
        </el-form-item>
        <el-form-item prop="appSecret">
          <template v-slot:label>AppSecret <el-tooltip class="item" effect="dark" content="AppSecret" placement="top-start"><i class="el-icon-info" /></el-tooltip></template>
          <el-input v-model="model.appSecret" placeholder="AppSecret"></el-input>
        </el-form-item>
        <el-form-item prop="token">
          <template v-slot:label>微信验证TOKEN <el-tooltip class="item" effect="dark" content="微信验证TOKEN" placement="top-start"><i class="el-icon-info" /></el-tooltip></template>
          <el-input v-model="model.token" placeholder="微信验证TOKEN"></el-input>
        </el-form-item>
        <el-form-item>
          <template v-slot:label>消息加解密方式 <el-tooltip class="item" effect="dark" content="如需使用安全模式请在管理中心修改，仅限服务号和认证订阅号" placement="top-start"><i class="el-icon-info" /></el-tooltip></template>
          <el-radio-group v-model="model.encodeType">
            <el-radio :label="0">明文模式</el-radio>
            <el-radio :label="1">兼容模式</el-radio>
            <el-radio :label="2">安全模式</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="encodingAESKey">
          <template v-slot:label>EncodingAESKey <el-tooltip class="item" effect="dark" content="公众号消息加解密Key,在使用安全模式情况下要填写该值，请先在管理中心修改，然后填写该值，仅限服务号和认证订阅号" placement="top-start"><i class="el-icon-info" /></el-tooltip></template>
          <el-input v-model="model.encodingAESKey" placeholder="公众号消息加解密Key,在使用安全模式情况下要填写该值，请先在管理中心修改，然后填写该值，仅限服务号和认证订阅号"></el-input>
        </el-form-item>
        <el-form-item>
          <template v-slot:label>公众号关注二维码 <el-tooltip class="item" effect="dark" content="公众号关注二维码(尺寸:363*363)" placement="top-start"><i class="el-icon-info" /></el-tooltip></template>
          <div class="upLoadPicBox">
            <div v-if="model.qrCoreImage" class="pictrue">
              <el-image ref="LargePreView" class="imgHover" :src="$uploadURL(model.qrCoreImage)" :preview-src-list="[$uploadURL(model.qrCoreImage)]"></el-image>
              <div class="upload_cover_preview">
                <i @click.stop="largePreview('LargePreView')" class="iconfont icon-yanjing"></i>
                <i @click.stop="delLarge('qrCoreImage')" class="el-icon l-upload el-icon-delete"></i>
              </div>
            </div>
            <div @click="tabChangeGood" v-else class="upLoad"><i class="el-icon l-upload el-icon-camera-solid"></i></div>
          </div>
        </el-form-item>
        <el-form-item>
          <template v-slot:label>公众号分享二维码 <el-tooltip class="item" effect="dark" content="选择商城链接，扫描二维码直接进入商城对应页面，选择关注公众号，扫描二维码之后先进入公众号内，由公众号给用户推送对应页面的链接给客户进行点击进入" placement="top-start"><i class="el-icon-info" /></el-tooltip></template>
          <el-radio-group v-model="model.shareQrcodeType">
            <el-radio :label="0">商城链接</el-radio>
            <el-radio :label="1">关注公众号</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <template v-slot:label>推广海报有效期 <el-tooltip class="item" effect="dark" content="推广海报有效期：30天或永久" placement="top-start"><i class="el-icon-info" /></el-tooltip></template>
          <el-radio-group v-model="model.posterTimeType">
            <el-radio :label="0">30天</el-radio>
            <el-radio :label="1">永久</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <template v-slot:label>用户H5默认头像 <el-tooltip class="item" effect="dark" content="用户H5默认头像尺寸(80*80)" placement="top-start"><i class="el-icon-info" /></el-tooltip></template>
          <div class="upLoadPicBox">
            <div v-if="model.h5AvatarImage" class="pictrue">
              <el-image ref="H5AvatarLargePreView" class="imgHover" :src="$uploadURL(model.h5AvatarImage)" :preview-src-list="[$uploadURL(model.h5AvatarImage)]"></el-image>
              <div class="upload_cover_preview">
                <i @click.stop="largePreview('H5AvatarLargePreView')" class="iconfont icon-yanjing"></i>
                <i @click.stop="delLarge('h5AvatarImage')" class="el-icon l-upload el-icon-delete"></i>
              </div>
            </div>
            <div @click="onH5AvatarImageClick" v-else class="upLoad"><i class="el-icon l-upload el-icon-camera-solid"></i></div>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit('refOfficial')">立即提交</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </div>
</template>

<script>
import {
  getOfficialListApi,
  PUTOfficialListApi
} from '@/api/setting'

export default {
  name: 'Official',
  data() {
    return {
      grid: {
        xl: 9,
        lg: 12,
        md: 12,
        sm: 12,
        xs: 24
      },
      model: {
        appId: '',
        appSecret: '',
        token: '',
        encodeType: 0,
        encodingAESKey: '',
        qrCoreImage: '',
        shareQrcodeType: 0,
        posterTimeType: 0,
        h5AvatarImage: ''
      },
      rules: {
        appId: [
          { required: true, message: 'AppId不能为空', trigger: 'blur' }
        ],
        appSecret: [
          { required: true, message: 'AppSecret不能为空', trigger: 'blur' }
        ],
        token: [
          { required: true, message: 'Token不能为空', trigger: 'blur' }
        ],
        encodingAESKey: [
          { required: true, message: '公众号消息加解密Key不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getOfficialList()
  },
  methods: {
    // 删除图片
    delLarge(name) {
      this.$set(this.model, name, '');
    },
    // 大图预览
    largePreview(ref) {
      this.$refs[ref].clickHandler();
    },
    // H5头像
    onH5AvatarImageClick() {
      const _this = this
      this.$modalUpload((data) => {
        _this.model.h5AvatarImage = data[0].path
      }, '1', '', '请选择用户H5默认头像')
    },
    // 公众号二维码
    tabChangeGood() {
      const _this = this
      this.$modalUpload((data) => {
        _this.model.qrCoreImage = data[0].path
      }, '1', '', '请选择公众号关注二维码')
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          PUTOfficialListApi(this.model).then(() => {
            this.getOfficialList()
            this.$message.success('修改成功！')
          })
        } else {
          this.$message.error('验证失败！')
          return false
        }
      })
    },
    getOfficialList() {
      getOfficialListApi().then(res => {
        this.$set(this, 'model', res)
      }).catch(() => this.$message.error('获取小程序配置失败！'))
    },
    previewBtn(refName) {
      this.$refs[refName].preview()
    }
  }
}
</script>

<style lang="scss" scoped>
  .official {
    .uploadBox {
      .upload-Box-files {
        width: 58px;
        height: 58px;
        position: relative;
        &.upload-Box-files:hover {
          div.upload-cover {
            opacity: 1;
            display: flex;
          }
        }
        .upload-cover {
          opacity: 0;
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          background: rgba(0, 0, 0, .6);
          transition: opacity .3s;
          color: #fff;
          /*display: flex;*/
          display: none;
          align-items: center;
          justify-content: center;
          svg {
            font-size: 17px;
            cursor: pointer;
            margin: 0 4px;
            vertical-align: middle;
          }
        }
      }
      .uploadBtn {
        width: 58px;
        height: 58px;
        border: 1px dashed #c0ccda;
        background-color: #ffffff;
        border-radius: 4px;
        overflow: hidden;
        box-sizing: border-box;
        text-align: center;
        cursor: pointer;
        &.uploadBtn:hover {
          box-shadow: 2px 2px 5px rgb(0 0 0 / 10%);
        }
        .el-icon-plus {
          line-height: 58px;
          font-size: 20px;
          vertical-align: middle;
          display: inline-block;
        }
      }
    }
    ::v-deep .viewCurrent {
      .l-del {
        font-size: 17px !important;
      }
    }
  }
</style>
